<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <el-button type="primary" icon="plus" @click="addHandler"/>
          <el-button type="primary" icon="edit" @click="editHandler"/>
          <el-button type="primary" icon="delete" @click="delHandler"/>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getSysBanks" height="300" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column prop="bankName" label="银行名称" min-width="15%"/>
      <el-table-column prop="singleLimit" label="单笔限额(元)" min-width="15%"/>
      <el-table-column prop="dayLimit" label="单日限额(元)" min-width="15%"/>
      <el-table-column prop="monthLimit" label="单月限额(元)" min-width="15%"/>
      <el-table-column prop="state" label="状态" min-width="20%" :formatter="formatterStateHandler"/>
      <el-table-column inline-template :context="_self" label="操作" width="100" fixed="right">
      <span>
        <el-button :type="row.state === '0'?'danger':'info'" size="small"
                   @click="onlineHandler($index,row)">{{row.state === '0' ? '上线' : '下线'}}</el-button>
      </span>
      </el-table-column>
    </el-table>


    <upload-form ref="uploadForm" :mode="formMode" :param="currentRecord"/>

    <!--<button v-clipboard="copyData" @success="copySucHandler" @error="copyErrorHandler"-->
    <!--ref="btnCopy" style="display:none">copy-->
    <!--</button>-->


  </div>
</template>

<script type="text/babel">
  import {mapActions} from 'vuex'

  import bankStore from './bll/bankStore'
  import store from '../../store'

  (!store.state.bankStore) && store.registerModule('bankStore', bankStore)


  import UploadForm from './uploadForm.vue';


  export default {
    data() {
      return {
        form: {
          region: ''
        },
        formMode: null,
        currentRecord: null
        //form: {},
      }
    },
    components: {UploadForm},
    computed: {
      getSysBanks() {
        return this.$store.state.bankStore.banks;
      }
    },

    mounted() {
      console.log('file upload page mounted.');
      this.initPage();
    },

    methods: {
      ...mapActions(['queryBanks', 'addBank', 'delBank', 'editBank']),

      initPage() {
        this.queryBanks();
      },
      openUploadDialogHandler() {
        this.$refs.uploadDialog.open();
      },
      addHandler() {
        this.$refs.uploadForm.enable();
        this.formMode = 'ADD';
        this.$refs.uploadForm.clear();
      },
      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$refs.uploadForm.enable();
        this.formMode = 'EDIT';
      },
      delHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }
        this.$confirm('确定删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var id = this.currentRecord.id
          this.delBank({id}).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            // this.queryBanks()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      onlineHandler(index, row) {
        this.$confirm(row.state === '0' ? '你确定要上线吗?' : '你确定要下线吗?', '确定', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var editdata = row
          if (row.state === '0') {
            editdata.state = '1'
          } else if (row.state === '1') {
            editdata.state = '0'
          }
          this.editBank({formData: editdata}).then(data => {
            this.$message.success(row.state === '0' ? '下线成功' : '上线成功');
            //this.initPage()
          });
        });
      },
      tableRowChangeClickHandler(currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
        this.$refs.uploadForm.disable();
        this.formMode = 'EDIT';
      },
      formatterStateHandler(row, column) {
        switch (column.property) {
          case 'state':
            switch (row.state) {
              case '1':
                return '上线';
              case '0':
                return '下线';
            }
            break;
        }
        return 'Unkown key'
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

